$(function () {
    var lastIndex;
    var topMenus;
    var icons;
    $.get("/menu/icons.do", function (data, status) {
        icons = data;
        $.get("/menu/getTopMenu.do", function (data, status) {
            topMenus = data;
            $("#hrms_menu_list").datagrid({
                title:"菜单管理",
                iconCls:"icon-tip",
                width:"auto",
                height:"auto",
                nowrap:false,
                striped:true,
                fit:true,
                url:"/menu/menuList.do",
                method:"get",
                sortName:"id",
                sortOrder:"desc",
                idField:"id",
//                singleSelect:true,
                columns:[
                    [
                        {title:"id", field:"id", width:0, editor:"text"},{field:'ck',checkbox:true},
                        {title:"栏目名称", field:"name", width:150, align:"center", sortable:true, editor:"text"},
                        {title:"父级栏目", field:"pid", width:150, align:"center", formatter:function (value, rec) {
                            for (var i = 0; i < topMenus.length; i++) {
                                if (topMenus[i].id == value) return topMenus[i].name;
                            }
                            return "";
                        }, sortable:true, editor:{type:"combobox", options:{valueField:"id", textField:"name", data:topMenus, required:true}}},
                        {title:"栏目图标", field:"icon", width:150, align:"center", editor:{type:"combotree",options:{url:"/menu/iconsTree.do"}}, formatter:function(value,rec){
                            return '<span class="icon '+value+'" >&nbsp;</span>';
                        }},
                        {title:"栏目链接", field:"url", width:300, editor:"text"}
                    ]
                ],
                pagination:true,
                rownumbers:true,
                toolbar:[
                    {
                        text:"增加",
                        iconCls:"icon-add",
                        handler:function () {
                            $("#hrms_menu_list").datagrid("endEdit", lastIndex);
                            $("#hrms_menu_list").datagrid("appendRow", {
                                id:"",
                                name:"",
                                pid:null,
                                icon:"icon-sys",
                                url:""
                            });
                            lastIndex = $("#hrms_menu_list").datagrid("getRows").length - 1;
                            $("#hrms_menu_list").datagrid("selectRow", lastIndex);
                            $("#hrms_menu_list").datagrid("beginEdit", lastIndex);
                        }
                    },
                    "-",
                    {
                        text:"删除",
                        iconCls:"icon-remove",
                        handler:function () {
                            var rows = $('#hrms_menu_list').datagrid('getSelections');
                            for(var i=rows.length-1;i>=0;i--){
                                var index = $("#hrms_menu_list").datagrid("getRowIndex", rows[i]);
                                $("#hrms_menu_list").datagrid("deleteRow", index);
                            }
                        }
                    },
                    "-",
                    {
                        text:"保存修改",
                        iconCls:"icon-save",
                        handler:function () {
                            $("#hrms_menu_list").datagrid("endEdit", lastIndex);
                            var addRows = $("#hrms_menu_list").datagrid("getChanges", "inserted");
                            var delRows = $("#hrms_menu_list").datagrid("getChanges", "deleted");
                            var updateRows = $("#hrms_menu_list").datagrid("getChanges", "updated");
                            var reload = false;
                            for (var i = 0; i < addRows.length; i++) {
                                reload = true;
                                $.ajax({
                                    url:"/menu.do",
                                    type:"post",
                                    data:{
                                        name:addRows[i].name,
                                        pid:addRows[i].pid,
                                        icon:addRows[i].icon,
                                        url:addRows[i].url
                                    }
                                });
                            }
                            for (var i = 0; i < delRows.length; i++) {
                                reload = true;
                                $.ajax({
                                    url:"/menu/" + delRows[i].id + ".do",
                                    type:"delete"
                                });
                            }
                            for (var i = 0; i < updateRows.length; i++) {
                                $.ajax({
                                    url:"/menu/" + updateRows[i].id + ".do",
                                    type:"post",
                                    data:{
                                        id:updateRows[i].id,
                                        name:updateRows[i].name,
                                        pid:updateRows[i].pid,
                                        icon:updateRows[i].icon,
                                        url:updateRows[i].url
                                    }
                                });
                            }
                            $("#hrms_menu_list").datagrid("acceptChanges");
                            showTips("提示消息", "保存成功！");
                            if (reload)
                                $("#hrms_menu_list").datagrid("reload");
                        }
                    },
                    "-",
                    {
                        text:"撤销修改",
                        iconCls:"icon-undo",
                        handler:function () {
                            $("#hrms_menu_list").datagrid("rejectChanges");
                        }
                    }
                ],
                onClickRow:function (rowIndex) {
                    if (lastIndex != rowIndex) {
                        $("#hrms_menu_list").datagrid("endEdit", lastIndex);
                    }
                },
                onDblClickRow:function (rowIndex) {
                    if (lastIndex != rowIndex) {
                        $("#hrms_menu_list").datagrid("endEdit", lastIndex);
                        $("#hrms_menu_list").datagrid("beginEdit", rowIndex);
                    }
                    lastIndex = rowIndex;
                },
                onBeforeLoad:function () {
                    $(this).datagrid("rejectChanges");
                }
            });
            $('#hrms_menu_list').datagrid('hideColumn', "id");
        });

    });
});